import tkinter as tk
from tkinter import font, END, ANCHOR, filedialog
from PIL import Image, ImageTk
from logic import TaskManager  # 导入逻辑模块
from tkcalendar import DateEntry  # 导入tkcalendar中的DateEntry

# UI相关设置
default_font = ("Times", 20)

# 创建任务管理器实例
task_manager = TaskManager()

# 定义函数
def add_task():
    task = entry_task.get()
    priority = priority_var.get()
    description = entry_description.get()
    date = cal.get_date()  # 获取选定的日期
    if task:
        task_manager.add_task(priority, description, date, task)
        listbox_tasks.insert(END, f"{priority} | {description} | {date} | {task}")
    entry_task.delete(0, END)
    entry_description.delete(0, END)

def delete_task():
    selected_index = listbox_tasks.curselection()
    if selected_index:
        index = selected_index[0]
        task_manager.delete_task(index)
        listbox_tasks.delete(index)

def edit_task():
    selected_index = listbox_tasks.curselection()
    if selected_index:
        index = selected_index[0]
        priority, description, date, task = task_manager.task_list[index]
        entry_task.delete(0, END)
        entry_description.delete(0, END)
        
        entry_task.insert(0, task)
        entry_description.insert(0, description)
        cal.set_date(date)  # 设置日期选择器为当前任务的日期
        priority_var.set(priority)
        
        listbox_tasks.delete(index)
        task_manager.edit_task(index, priority, description, date, task)

def add_photo():
    global image_path
    image_path = filedialog.askopenfilename(title="choose a photo", filetypes=(("Image Files", "*.png;*.jpg;*.jpeg;*.gif;*.bmp"), ("All Files", "*.*")))
    if image_path:
        print(f"Selected image path: {image_path}")  
        display_image(image_path)
    else:
        print("No image selected")

def display_image(path):
    try:
        img = Image.open(path)
        print(f"Image opened successfully, size: {img.size}")  
        img.thumbnail((200, 200))
        img_tk = ImageTk.PhotoImage(img)
        label_image.config(image=img_tk)
        label_image.image = img_tk  
    except Exception as e:
        print(f"Error loading image: {e}")

# 初始化UI
window = tk.Tk()
window.title("To-Do List")

# 定义UI元素
generaltitle = tk.Label(window, text="Title:", font=default_font)
generaltitle.grid(row=0, column=0, padx=5, pady=5)

entry_task = tk.Entry(window, width=30, font=default_font)
entry_task.grid(row=0, column=1, padx=5, pady=5)

label_priority = tk.Label(window, text="Priority:", font=default_font)
label_priority.grid(row=1, column=0, padx=5, pady=5)

priority_var = tk.StringVar(value="Low")
option_menu_priority = tk.OptionMenu(window, priority_var, "Low", "Medium", "High")
option_menu_priority.grid(row=1, column=1, padx=5, pady=5)
option_menu_priority.config(font=("Times", 20))

label_description = tk.Label(window, text="Description:", font=default_font)
label_description.grid(row=2, column=0, padx=5, pady=5)

entry_description = tk.Entry(window, width=30, font=default_font)
entry_description.grid(row=2, column=1, padx=5, pady=5)

label_date = tk.Label(window, text="Date: ", font=default_font)
label_date.grid(row=3, column=0, padx=5, pady=5)

cal = DateEntry(window, selectmode='day', date_pattern='yyyy-mm-dd', font=default_font)
cal.grid(row=3, column=1, padx=5, pady=5)

label_image = tk.Label(window, borderwidth=2, relief="solid")
label_image.grid(row=4, column=0, columnspan=2, padx=10, pady=10, sticky="nsew")

listbox_title = tk.Label(window, text="List:", font=default_font)
listbox_title.grid(row=4, column=0, padx=5, pady=5)

listbox_tasks = tk.Listbox(window, height=5, width=40, font=default_font)
listbox_tasks.grid(row=5, column=0, columnspan=2, padx=5, pady=5)

button_add_task = tk.Button(window, text="Add", font=default_font, command=add_task)
button_add_task.grid(row=6, column=0, padx=5, pady=5)

button_delete_task = tk.Button(window, text="Delete", font=default_font, command=delete_task)
button_delete_task.grid(row=6, column=1, padx=5, pady=5)

button_edit_task = tk.Button(window, text="Edit", font=default_font, command=edit_task)
button_edit_task.grid(row=7, column=0, padx=5, pady=5)

button_add_photo = tk.Button(window, text="Add picture", font=default_font, command=add_photo)
button_add_photo.grid(row=7, column=1, padx=5, pady=5)

for i in range(9):  
    window.grid_rowconfigure(i, weight=1)
window.grid_columnconfigure(0, weight=1)
window.grid_columnconfigure(1, weight=1)

# 加载初始任务
for task in task_manager.get_tasks():
    listbox_tasks.insert(END, task)

# 启动窗口
window.mainloop()